<script lang="ts" setup>
import type { EduSchoolContactApi } from '#/api/edu/base/schoolContact';

import { ref } from 'vue';

import { useVbenModal } from '@vben/common-ui';
import { IconifyIcon } from '@vben/icons';

import { Button } from 'ant-design-vue';

import { getSchoolContact } from '#/api/edu/base/schoolContact';
import { useDescription } from '#/components/description';

import { useDetailSchema } from '../data';

const formData = ref<EduSchoolContactApi.SchoolContact>();

const [Descriptions] = useDescription({
  bordered: true,
  size: 'middle',
  column: 1,
  labelWidth: 120,
  schema: useDetailSchema(),
});
const desensitize = ref(true);
async function handleDesensitize() {
  desensitize.value = !desensitize.value;
  const data = await getSchoolContact(formData.value.id, desensitize.value);
  formData.value = data;
}
const [Modal, modalApi] = useVbenModal({
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      formData.value = undefined;
      return;
    }
    let data = modalApi.getData<EduSchoolContactApi.SchoolContact>();
    if (!data || !data.id) {
      return;
    }
    modalApi.lock();
    try {
      data = await getSchoolContact(data.id, desensitize.value);
      formData.value = data;
    } finally {
      modalApi.unlock();
    }
  },
});
</script>

<template>
  <Modal
    title="查看 学校联系人"
    class="w-[50%]"
    :show-cancel-button="false"
    :show-confirm-button="false"
  >
    <Descriptions :data="formData" class="mx-4" />
    <template #prepend-footer>
      <Button type="primary" @click="handleDesensitize" :danger="desensitize">
        <IconifyIcon
          :icon="
            desensitize
              ? 'ant-design:eye-filled'
              : 'ant-design:eye-invisible-filled'
          "
        />
        {{ desensitize ? '关闭脱敏' : '启用脱敏' }}
      </Button>
    </template>
  </Modal>
</template>
